package com.anyang.mq.common.controller;

import com.anyang.mq.common.domain.Rocketmq;
import com.anyang.mq.common.service.RocketmqService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author  anyang
 * Email: 1300378587@qq.com
 * Created Date:2019-11-13 11:37
 */
@RestController
@Slf4j
public class RocketmqController{
    @Autowired
    RocketmqService rocketmqService;
    @Autowired
    RocketMQTemplate rocketMQTemplate;
    public static final String RESULT_SUCCESS = "成功";
    public static final String RESULT_FAILED = "失败";

    @Value("${rocketmq.producer.group}")
    String group;

     @GetMapping("msgList")
    public List<Rocketmq> getMessageList(){
        return  rocketmqService.list();
    }

    @GetMapping("msgSend")
    public String sendMsg() throws InterruptedException{
        /**
         * 这个是原生rocketMQTemplate.convertAndSend("springboot-rocketmq" , "消息提供方来过");
         */
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
//        rocketMQTemplate.createAndStartTransactionMQProducer()
        //创建消息，设置topic和body
        Message  msg = new Message("xst-mq","消息生产着".getBytes());
        //发送消息
        Rocketmq rocketmq = sendMessage(producer,msg);

        //保存到数据库
        boolean saveResult = rocketmqService.save(rocketmq);

        log.info("消息发送完毕,消息保存： "+(saveResult?RESULT_SUCCESS:RESULT_FAILED)+ LocalDateTime.now());
        return RESULT_SUCCESS+LocalDateTime.now();
    }

    private Rocketmq sendMessage(DefaultMQProducer producer,Message  msg) throws InterruptedException{
        //创建记录保存对象
        String localResult = "";
        Rocketmq rocketmq = new Rocketmq();
        rocketmq.setInfo("msg send");
        rocketmq.setType("0");
        SendResult result = null ;
        try{
            producer.setProducerGroup(group);
            //发送消息
            result = producer.send(msg);
            localResult = localResult+result;
            rocketmq.setResult(localResult);
        }catch(RemotingException e){
            rocketmq.setResult(e.getMessage());
            log.error("Remoting Exception ",e);
        }catch(MQClientException e){
            rocketmq.setResult(e.getMessage());
            log.error("MQClient Exception ",e);
        }catch(MQBrokerException e){
            rocketmq.setResult(e.getMessage());
            log.error("MQBroker Exception ",e);
        }
        return  rocketmq;
    }
}
